﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AWA.VAT.DbAccess.SQL;
using AWA.VAT.DbAccess.Excel;

namespace AWA.VAT.GuestRelations.Models
{
    public class LoadModel
    {
        private SQLLookup _lookup;
        private ExcelToSQLReader excel;

        #region Events
        public event EventHandler ExcelFileReadStarted;
        private void RaiseExcelFileReadStarted()
        {
            EventHandler eh = ExcelFileReadStarted;
            if (eh != null)
                eh(this, EventArgs.Empty);
        }

        public event EventHandler ExcelFileReadCompleted;
        private void RaiseExcelFileReadCompleted()
        {
            EventHandler eh = ExcelFileReadCompleted;
            if (eh != null)
                eh(this, EventArgs.Empty);
        }

        public event EventHandler SaveStarted;
        private void RaiseSaveStarted()
        {
            EventHandler eh = SaveStarted;
            if (eh != null)
                eh(this, EventArgs.Empty);
        }

        public event EventHandler SaveCompleted;
        private void RaiseSaveCompleted()
        {
            EventHandler eh = SaveCompleted;
            if (eh != null)
                eh(this, EventArgs.Empty);
        } 
        #endregion
        
        public LoadModel(SQLLookup lookup)
        {
            _lookup = lookup;
        }

        public void LoadExcelFileContents(string fileLocation)
        {
            // Read data from Excel
            RaiseExcelFileReadStarted();
            excel = new ExcelToSQLReader(fileLocation, new SQLLookup());
            excel.ReadFile();
            RaiseExcelFileReadCompleted();
        }

        public void CommitFileContentsToDB()
        {
            throw new NotImplementedException();

            RaiseSaveStarted();

            if (excel == null)
                return;
            
            // save editors
            foreach (Editor editor in excel.Editors.Values)
            {
                // query for existing editor
                Editor candidate = _lookup.GetEditorByEmail(editor.Email);
                // if found, copy values and save
                // else, just save
            }
            // save attendances

            // save sources

            // save artists

            // save songs

            // save AMVs

            // save entries


            RaiseSaveCompleted();
        }
    }
}
